﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_MoveTeam]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_MoveTeam];
GO
CREATE PROCEDURE [dbo].[sproc_MoveTeam] 
    @FromTeamID int,
    @ToTeamID int
/*

============================================================
功能:    移动组
参数:
    @FromTeamID int    :    被移动的源组（类）ID
    @ToTeamID int        :    移动到的目标组（类）ID

============================================================

*/
AS
--判断ToTeamID是否是FromTeamID的子ID
DECLARE @id     int
DECLARE @pid     int

--在使用临时表时加上，否则无法正确返回记录
SET NOCOUNT ON

SET @pid=@ToTeamID
SET @id =0

--得到所有被访问组的父组
CREATE TABLE #pteam (pid int)
INSERT INTO #pteam VALUES(@pid)
WHILE (@pid<>@id)
BEGIN
    SET @id = @pid
    SELECT @pid = classparentid FROM uds_class WHERE classid = @id
    INSERT INTO #pteam VALUES(@id)    
End

IF EXISTS(SELECT 1 FROM #pteam WHERE pid = @FromTeamID)
BEGIN
    PRINT '不能移动到子项目中去~~'
    RETURN -1
END

UPDATE uds_class 
    SET classparentid = @toteamid 
    WHERE classid = @fromteamid
    
    RETURN 0

SET NOCOUNT OFF